/**
 * @file     reverseVowels.ts
 * @brief    [345. 反转字符串中的元音字母](https://leetcode.cn/problems/reverse-vowels-of-a-string/description/)
 * @author   Zhu
 * @date     2023-02-13 23:48
 */

function reverseVowels(s: string): string {
    const str = s.split('');

    function isVowel(char: string) {
        return ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'].includes(
            char
        );
    }

    let i = 0;
    let j = str.length - 1;
    while (i < j) {
        while (i < j && !isVowel(str[i])) {
            i++;
        }

        while (i < j && !isVowel(str[j])) {
            j--;
        }

        if (i < j) {
            [str[i], str[j]] = [str[j], str[i]];
            i++;
            j--;
        }
    }

    return str.join('');
}
